<!-- /*
 * @Author: RDSunday 
 * @Date: 2020-03-21 10:08:20 
 * @Last Modified by:   RDSunday 
 * @Last Modified time: 2020-03-21 10:08:20 
 */ -->
<!-- 点击式导航栏下拉菜单 -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            overflow: hidden;
            background-color: #333;
            font-family: Arial, Helvetica, sans-serif;
        }
        .container a {
            float: left;
            font-size: 16px;
            color: white;
            text-decoration: none;
            text-align: center;
            padding: 14px 16px;

            
        }
        .dropdown {
            float: left;
            /* overflow: hidden; */

        }
        .dropdown .dropbtn {
            cursor: pointer;
            font-size: 16px;
            border: none;
            outline: none;
            color: white;
            padding: 14px 16px;
            /* 继承父级属性 颜色#333*/
            background-color: inherit;
        }
        .container a:hover, .dropdown:hover .dropbtn {
            background-color: red;
        }
        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #f9f9f9;
            min-width: 160px;
            box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
            z-index: 1;
        }
        .dropdown-content a {
            float: none;
            color: black;
            padding: 12px 16px;
            display: block;
            text-decoration: none;
            text-align: left;
        }
        .dropdown-content a:hover {
            background-color: #ddd;
        }
        .show {
            display: block;
        }
        /* 右边下拉菜单 */
        .dropbtn2 {
            background-color: #4caf50;
            color: white;
            padding: 16px;
            font-size: 16px;
            border: none;
            cursor: pointer;
        }
        .dropbtn2:hover, dropbtn2:focus {
            background-color: #3e8e41;

        }
        .dropdown2 {
            float: right;
            position:relative;
            display: inline-block;
        }
        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #f9f9f9;
            min-width: 160px;
            overflow: auto;
            right: 0;
            z-index: i;
        }
        .dropdown-content2 a{
            color: black;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
        }
        .dropdown a:hover {
            background-color: #f1f1f1;
        }
        .show { display:block}
    </style>
</head>
<body>
    <div class="container">
        <a href="#home">主页</a>
        <a href="#news">新闻</a>
        <div class="dropdown">
            <button class="dropbtn" onclick="myFunction()">下拉菜单</button>
            <div class="dropdown-content" id="myDropdown">
                <a href="#">Link 1</a>
                <a href="#">Link 2</a>
                <a href="#">Link 3</a>
            </div>
        </div>
    </div>
    <!-- 右边下拉菜单 -->
    <div class="dropdown2">
        <button onclick="myFunction2()" class="dropbtn2">Dropdown</button>
        <div id="myDropdown2" class="dropdown-content2">
            <a href="#home">Home</a>
            <a href="#aboout">About</a>
            <a href="#contact">Contact</a>
        </div>
    </div>
    <h3>导航栏中的下拉菜单</h3>
    <p>点击按钮显示下拉菜单</p>
    <script>
        // 点击按钮，下拉菜单在显示/隐藏之间切换
        function myFunction() {
            document.getElementById("myDropdown").classList.toggle("show");
        }
        // 点击下拉菜单意外区域隐藏
        window.onclick = function(e) {
            if(!e.target.matches('.dropbtn')) {
                var myDropdown = document.getElementById("myDropdown");
                if(myDropdown.classList.contains('show')) {
                    myDropdown.classList.remove('show');
                }
            }
        }
    </script>
</body>
</html>